﻿using System;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
using TSites.Core.Domain.UserMangerment;
using System.Collections.Generic;
using TSites.Administrator.Validation;

namespace TSites.Administrator.Models
{
    public class UserAddViewModel
    {
        [Required]
        [StringLength(20, MinimumLength=6)]
		[Remote("IsUserIdAvailable", "UserManagement")]
        [RegularExpression(@"(\S)+", ErrorMessage = "White space is not allowed.")]
        public string UserId { get; set; }

        [Required]
        public string Username { get; set; }

		[DateRangeAttribute()]
        public DateTime DateOfBirth { get; set; }

        [Required]
        public char Sex { get; set; }

        [Required]
		[Remote("IsEmailAvailable", "UserManagement")]
        [RegularExpression(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})\s*")]
        public string Email { get; set; }

        public string Address { get; set; }
        public string PhoneNumber { get; set; }

        public User Convert2User()
        {
            var addresses = new List<Address>();
            addresses.Add(new Address() {
                UserAddress = Address
            });

            var phones = new List<Phone>();
            phones.Add(new Phone() {
                PhoneNumber = PhoneNumber
            });

            User currentUser = new User() {
                ID = UserId,
                UserName = Username,
                Sex = Sex.ToString(),
                Email = Email,
                Addresses = addresses,
                Phones = phones
            };

			if (DateOfBirth != DateTime.MinValue)
			{
				currentUser.DateOfBirth = DateOfBirth;
			}

			return currentUser;
        }
    }
}